package com.linkhouse.util;

import java.io.*;
import java.util.Base64;

/**
 * @author J-B-Y
 * @description
 * @date 2022/5/29-16:02
 */
public class ImageUtil {
    /**
     * @param faceBase base64编码字符串
     * @return
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     * @Param file
     */
    public static void uploadImage(String faceBase, File file) {
        // 解密base64
        Base64.Decoder decoder = Base64.getDecoder();
        // 去掉base64前缀 data:image/jpeg;base64
        faceBase = faceBase.substring(faceBase.indexOf(",", 1) + 1, faceBase.length());


        byte[] b = decoder.decode(faceBase);
        // 处理数据
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        // 保存图片
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            out.write(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
